package com.sinodata.bsm.cicp.finder.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: 双机--> 节点--> 接口 的发现程序，用于发现指定指定节点下的接口名称
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:15:11   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HAInterfaceFinder extends Finder {

    private static final Logger logger = Logger.getLogger(HAInterfaceFinder.class);

    /**
     * 发现程序
     * 
     * @param pResValue  父资源的信息
     * @param pattrVals  父资源属性
     * @param attributes 资源自带的属性
     * @return 资源列表
     */
    /* @Override
     public List<List<Serializable>> find(ResValue pResValue, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) {
         String shell = "clstat -o";
         BufferedReader reader = null;
         try {
             reader = CollectorUtil.getReader(shell, host);
             return this.parseReader(reader, pattrVals, attributes);
         } catch (IOException e) {
             logger.error("Failed to auto find HAInterface ", e);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e) {
                     logger.error("Failed to close IO resource ", e);
                 }
             }
         }
         return new ArrayList<List<Serializable>>();
     }*/

    /**
     * 解析命令响应结果
     * 
     * @param reader 命令响应结果
     * @param pattrVals  接口父资源 节点的属性信息
     * @param attributes 资源自带的属性
     * @return 资源列表
     * @throws IOException
     */
    /* private List<List<Serializable>> parseReader(BufferedReader reader, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) throws IOException {
         List<List<Serializable>> tlqResources = null;
         // 匹配：Node : xxx State : xxx
         Pattern typePatn = Pattern.compile("\\s*Node\\s*:\\s*(\\S*)\\s*State\\s*:\\s*(\\S*)\\s*");
         // 匹配： Interface : xxx Address : xxx
         Pattern codePatn = Pattern.compile("\\s*Interface\\s*:\\s*([^\\(]*).*Address\\s*:\\s*(\\S*)\\s*");
         String tmp = null;
         List<Serializable> resAndAttr = null;
         Matcher typeMach = null;
         Matcher codeMach = null;
         while ((tmp = reader.readLine()) != null) {
             typeMach = typePatn.matcher(tmp);
             codeMach = codePatn.matcher(tmp);
             if (codeMach.matches() && tlqResources != null) {
                 resAndAttr = new ArrayList<Serializable>();
                 resAndAttr.add(findRes(codeMach.group(1)));
                 resAndAttr.add(findAttr(attributes, 0, codeMach.group(1)));
                 tlqResources.add(resAndAttr);
             } else if (typeMach.matches()) {// 只找指定节点下的 接口
                 if (pattrVals[0].getAttrValue().indexOf(typeMach.group(1)) > -1) {
                     tlqResources = new ArrayList<List<Serializable>>();
                 } else if (tlqResources != null) {
                     break;
                 }
             }
         }
         return tlqResources;
     }*/

    /**
     * 创建ResValue
     * 
     * @param busCode 资源名称
     * @return ResValue
     */
    /* private ResValue findRes(String busCode) {
         ResValue resValue = new ResValue();
         resValue.setIp(host);
         resValue.setName(busCode);
         resValue.setInternalId(busCode);
         resValue.setDescript("Interface info");
         return resValue;
     }*/

    /**
     * 创建资源的属性
     * 
     * @param attributes
     * @param i  属性数组的角标
     * @param busCode 设置给属性的值
     * @return
     */
    /*private ResAttributeValValue findAttr(ResAttributeValue[] attributes, int i, String busCode) {
        ResAttributeValValue attrValValue = new ResAttributeValValue();
        attrValValue.setResAttributeId(attributes[i].getId());
        attrValValue.setAttrValue(busCode);
        return attrValValue;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String shell = "clstat -o";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            return parseReader(reader, params);
        } catch (IOException e) {
            logger.error("Failed to auto find HAInterface ", e);
            return new ArrayList<String[]>();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    private List<String[]> parseReader(BufferedReader reader, Map params) throws IOException {
        String interfaceName = (String) params.get("INTERFACENAME");
        List<String[]> list = null;
        // 匹配：Node : xxx State : xxx
        Pattern typePatn = Pattern.compile("\\s*Node\\s*:\\s*(\\S*)\\s*State\\s*:\\s*(\\S*)\\s*");
        // 匹配： Interface : xxx Address : xxx
        Pattern codePatn = Pattern.compile("\\s*Interface\\s*:\\s*([^\\(]*).*Address\\s*:\\s*(\\S*)\\s*");
        String tmp = null;
        Matcher typeMach = null;
        Matcher codeMach = null;
        while ((tmp = reader.readLine()) != null) {
            typeMach = typePatn.matcher(tmp);
            codeMach = codePatn.matcher(tmp);
            if (codeMach.matches() && list != null) {
                String[] info = new String[4];
                list.add(info);
                info[0] = codeMach.group(1);
                info[1] = codeMach.group(1);
                info[2] = "Interface info";
            } else if (typeMach.matches()) {// 只找指定节点下的 接口
                if (interfaceName.indexOf(typeMach.group(1)) > -1) {
                    list = new ArrayList<String[]>();
                } else if (list != null) {
                    break;
                }
            }
        }
        return list;
    }

}
